package com.vm.myview;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DecimalFormat;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.vm.activity.DetailActivity;
import com.vm.activity.OrderDetailActivity;
import com.vm.commons.VMConst;
import com.vm.model.ActiveSoft;
import com.vm.model.Comment;
import com.vm.model.Goods;
import com.vm.model.Soft;
import com.vm.model.UserInfo;
import com.vm.model.UserOrder;
import com.vm.utils.JsonUtils;
import com.vm.utils.MyDisplayImageOptions;
import com.vm.utils.MyVolley;
import com.vm.utils.SoftDataUtil;
import com.wwtscapp.apk.R;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年3月27日 下午2:17:00]
 * @Version: [v1.0]
 * 
 */
@SuppressLint("InflateParams")
public class ViewUtils {

	Activity activity;
	private RequestQueue mQueue;

	// ImageListener listener;
	// ImageLoader mImageLoader;

	public ViewUtils(Context context) {
		this.activity = (Activity) context;

	}

	/**
	 * 动态创建LinearLayout
	 * 
	 * @param i
	 * @param activeSoft
	 * @param progress
	 *            1.初始化进度条,0不初始化进度条
	 * @return
	 */
	public LinearLayout dyAddView(int i, final ActiveSoft activeSoft,
			int progress) {
		mQueue = MyVolley.getRequestQueue();
		LinearLayout linearLayout = (LinearLayout) activity.getLayoutInflater()
				.inflate(R.layout.found_item, null);
		if (i == 0) {
			// LruImageCache lruImageCache = LruImageCache.instance();
			// mImageLoader = MyVolley.getImageLoader();
		}

		Button btn_download = (Button) linearLayout
				.findViewById(R.id.btn_download);

		TextView tv_softName = (TextView) linearLayout
				.findViewById(R.id.tv_softName);
		TextView tv_download_number = (TextView) linearLayout
				.findViewById(R.id.tv_download_number);
		ImageView iv_softImage = (ImageView) linearLayout
				.findViewById(R.id.iv_softImage);
		TextView tv_softSize = (TextView) linearLayout
				.findViewById(R.id.tv_softSize);
		RatingBar rb_star = (RatingBar) linearLayout.findViewById(R.id.rb_star);
		final TextView tv_software_introduction = (TextView) linearLayout
				.findViewById(R.id.tv_software_introduction);
		String url = VMConst.DETAIL_URL + activeSoft.getSoftId();

		JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null,
				new Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject arg0) {
						// TODO Auto-generated method stub
						Soft soft = new Soft();
						soft = JsonUtils.jsonToBean(arg0, Soft.class);
						if (soft.getInAWord() != null) {
							tv_software_introduction.setText(soft.getInAWord());
						} else {
							tv_software_introduction.setText("");
						}
					}

				}, null);

		mQueue.add(jsonObjectRequest);
		if (activeSoft.getName() != null) {
			// String softName =
			// StringUtil.getShotName(soft.getName());
			tv_softName.setText(activeSoft.getName());
		}
		// 大小
		if (activeSoft.getSize() != null) {
			float size = activeSoft.getSize();
			DecimalFormat decimalFormat = new DecimalFormat(".00");// 构造方法的字符格式这里如果小数不足2位,会以0补足.
			String shotSize = decimalFormat.format(size);// format 返回的是字符串
			tv_softSize.setText(shotSize + "M");
		}
		// 星级
		if (activeSoft.getStar() != null) {
			float fstar = activeSoft.getStar();
			rb_star.setRating(fstar / 2);
		}

		// 隐藏标语
		// tv_software_introduction.setVisibility(View.GONE);
		// if (activeSoft.getInAWord() != null) {
		// tv_software_introduction.setText(activeSoft.getInAWord());
		// } else {
		// tv_software_introduction.setText("");
		// }

		if (activeSoft.getDownload() != null) {
			String download = SoftDataUtil.getSoftDownload(activeSoft
					.getDownload());
			tv_download_number.setText(download);
		}

		ImageLoader.getInstance().displayImage(
				VMConst.URL + activeSoft.getImageUrl(), iv_softImage,
				MyDisplayImageOptions.getInstance());

		OnClickListener ll_listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(activity, DetailActivity.class);

				intent.putExtra("softId", activeSoft.getSoftId());
				activity.startActivityForResult(intent, 10);
			}
		};

		linearLayout.setOnClickListener(ll_listener);

		/**
		 * 加载一些列下载有关的东西
		 */
		if (progress == 1) {
			new FoundDown(activity, linearLayout, activeSoft);
		} else {
			btn_download.setVisibility(View.GONE);
		}
		// downloadService.initProgressBar(soft);
		return linearLayout;
	}

	/**
	 * 动态创建LinearLayout(积分专区)
	 * 
	 * @param i
	 * @param activeSoft
	 * @param progress
	 *            1.初始化进度条,0不初始化进度条
	 * @return
	 */
	public LinearLayout dyAddView2(int i, final ActiveSoft activeSoft,
			int progress) {

		LinearLayout linearLayout = (LinearLayout) activity.getLayoutInflater()
				.inflate(R.layout.present1_ongoing_item, null);
		if (i == 0) {
			// LruImageCache lruImageCache = LruImageCache.instance();
			// mImageLoader = MyVolley.getImageLoader();
		}

		Button btn_download = (Button) linearLayout
				.findViewById(R.id.btn_download);

		TextView tv_softName = (TextView) linearLayout
				.findViewById(R.id.tv_softName);
		TextView tv_download_number = (TextView) linearLayout
				.findViewById(R.id.tv_download_number);
		ImageView iv_softImage = (ImageView) linearLayout
				.findViewById(R.id.iv_softImage);
		TextView tv_softSize = (TextView) linearLayout
				.findViewById(R.id.tv_softSize);
		RatingBar rb_star = (RatingBar) linearLayout.findViewById(R.id.rb_star);

		TextView tv_remaining_points_num = (TextView) linearLayout
				.findViewById(R.id.tv_remaining_points_num);

		TextView percent = (TextView) linearLayout
				.findViewById(R.id.tv_snap_up);

		ProgressBar pgbar_intergral = (ProgressBar) linearLayout
				.findViewById(R.id.progressBar_snap_up);

		if (activeSoft.getName() != null) {
			// String softName =
			// StringUtil.getShotName(soft.getName());
			tv_softName.setText(activeSoft.getName());
		}
		// 大小
		if (activeSoft.getSize() != null) {
			float size = activeSoft.getSize();
			DecimalFormat decimalFormat = new DecimalFormat(".00");// 构造方法的字符格式这里如果小数不足2位,会以0补足.
			String shotSize = decimalFormat.format(size);// format 返回的是字符串
			tv_softSize.setText(shotSize + "M");
		}
		// 星级
		if (activeSoft.getStar() != null) {
			float fstar = activeSoft.getStar();
			rb_star.setRating(fstar / 2);
		}

		if (activeSoft.getIntergral() != 0) {

			int remains = activeSoft.getIntergral()
					- activeSoft.getUsedIntergral();

			/**
			 * 当总积分大于已抢积分
			 */
			if (remains > 0) {
				int intergral = activeSoft.getIntergral();
				int used = activeSoft.getUsedIntergral();
				percent.setText(Integer.toString(used * 100 / intergral) + "%");

				pgbar_intergral.setMax(intergral);
				pgbar_intergral.setProgress(used);
				tv_remaining_points_num.setText(Integer.toString(remains));
			}
			// 活动已结束
			else {
				percent.setText("100%");
				pgbar_intergral.setMax(100);
				pgbar_intergral.setProgress(100);
				tv_remaining_points_num.setText("活动结束");
			}

		} else {
			tv_remaining_points_num.setText("0");
			percent.setText("100%");
			pgbar_intergral.setMax(1);
			pgbar_intergral.setProgress(1);
			progress = 0;
		}

		if (activeSoft.getDownload() != null) {
			String download = SoftDataUtil.getSoftDownload(activeSoft
					.getDownload());
			tv_download_number.setText(download);
		}

		ImageLoader.getInstance().displayImage(
				VMConst.URL + activeSoft.getImageUrl(), iv_softImage,
				MyDisplayImageOptions.getInstance());

		OnClickListener ll_listener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(activity, DetailActivity.class);

				intent.putExtra("softId", activeSoft.getSoftId());
				intent.putExtra("btnType", activeSoft.getBtnType());
				activity.startActivityForResult(intent, 10);
			}
		};

		linearLayout.setOnClickListener(ll_listener);

		/**
		 * 加载一些列下载有关的东西
		 */
		if (progress == 1) {
			new PresentDown(activity, linearLayout, activeSoft);
		} else {
			btn_download.setVisibility(View.GONE);
		}
		// downloadService.initProgressBar(soft);
		return linearLayout;
	}

	@SuppressLint({ "CutPasteId", "InflateParams" })
	public LinearLayout dyAddView3(int i, final UserOrder userOrder) {
		mQueue = MyVolley.getRequestQueue();
		LinearLayout linearLayout = (LinearLayout) activity.getLayoutInflater()
				.inflate(R.layout.order_item, null);
		final TextView tv_money = (TextView) linearLayout
				.findViewById(R.id.tv_money);
		final Button btn_check_logistics = (Button) linearLayout
				.findViewById(R.id.btn_check_logistics);
		final Button btn_confirm_goods = (Button) linearLayout
				.findViewById(R.id.btn_confirm_goods);
		final Button btn_comments = (Button) linearLayout
				.findViewById(R.id.btn_confirm_goods);
		final Button btn_finish = (Button) linearLayout
				.findViewById(R.id.btn_finish);
		final String status = "交易完成";
		String url = VMConst.REQUEST_URL + "goodsdetail?goodsId="
				+ userOrder.getGoodsId();
		JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null,
				new Listener<JSONObject>() {

					@Override
					public void onResponse(JSONObject arg0) {
						Goods goods = JsonUtils.jsonToBean(arg0, Goods.class);
						tv_money.setText(Float.toString(goods.getPrice()));
					}
				}, null);

		mQueue.add(jsonObjectRequest);
		if (userOrder.getOrderStatus().equals(status)) {
			btn_comments.setVisibility(View.VISIBLE);
			btn_finish.setVisibility(View.VISIBLE);
			btn_check_logistics.setVisibility(View.GONE);
			btn_confirm_goods.setVisibility(View.GONE);
		}
		btn_confirm_goods.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String status = "交易完成";
				try {
					status = URLEncoder.encode(status, "UTF-8");
				} catch (UnsupportedEncodingException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				String url = VMConst.REQUEST_URL + "updateOrderStatus?orderId="
						+ userOrder.getId() + "&status=" + status;
				StringRequest stringRequest = new StringRequest(url,
						new Listener<String>() {

							@Override
							public void onResponse(String arg0) {
								// TODO Auto-generated method stub
								btn_comments.setVisibility(View.VISIBLE);
								btn_finish.setVisibility(View.VISIBLE);
								btn_check_logistics.setVisibility(View.GONE);
								btn_confirm_goods.setVisibility(View.GONE);
							}

						}, null);
				mQueue.add(stringRequest);
			}
		});
		TextView tv_order_number = (TextView) linearLayout
				.findViewById(R.id.tv_order_number);
		TextView tv_time = (TextView) linearLayout.findViewById(R.id.tv_time);
		TextView tv_integra_num = (TextView) linearLayout
				.findViewById(R.id.tv_integra_num);
		linearLayout.findViewById(R.id.tv_money);
		TextView tv_goods_number = (TextView) linearLayout
				.findViewById(R.id.tv_goods_number);
		ImageView iv_goodsImage = (ImageView) linearLayout
				.findViewById(R.id.iv_goodsImage);
		if (userOrder.getId() != null) {
			tv_order_number.setText(userOrder.getId());
		}
		if (userOrder.getOrderTime() != null) {
			tv_time.setText(userOrder.getOrderTime());
		}
		if (userOrder.getIntegral() != null) {
			tv_integra_num.setText(Integer.toString(userOrder.getIntegral()));
		} else {
			tv_integra_num.setText("0");
		}
		if (userOrder.getNumber() != null) {
			tv_goods_number.setText(Integer.toString(userOrder.getNumber()));
		}
		linearLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(activity, OrderDetailActivity.class);
				intent.putExtra("orderId", userOrder.getId());
				activity.startActivity(intent);
			}
		});
		if (userOrder.getImageUrl() != null) {
			ImageLoader.getInstance().displayImage(
					VMConst.URL + userOrder.getImageUrl(), iv_goodsImage,
					MyDisplayImageOptions.getInstance());
		}
		return linearLayout;

	}

	@SuppressLint("InflateParams")
	public LinearLayout dyAddView4(int i, final Comment comment) {
		mQueue = MyVolley.getRequestQueue();
		LinearLayout linearLayout = (LinearLayout) activity.getLayoutInflater()
				.inflate(R.layout.comments_item, null);
		final ImageView iv_userImage = (ImageView) linearLayout
				.findViewById(R.id.iv_userImage);
		TextView tv_userName = (TextView) linearLayout
				.findViewById(R.id.tv_userName);
		TextView tv_content = (TextView) linearLayout
				.findViewById(R.id.tv_content);
		TextView tv_comment_time = (TextView) linearLayout
				.findViewById(R.id.tv_comment_time);
		RatingBar rb_star = (RatingBar) linearLayout.findViewById(R.id.rb_star);
		if (comment.getCStar() != null) {
			float fstar = comment.getCStar();
			rb_star.setRating(fstar / 2);
		}
		if (comment.getCName() != null) {
			tv_userName.setText(comment.getCName());
			String url = VMConst.REQUEST_URL + "searcher?username="
					+ comment.getCName();
			JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url,
					null, new Listener<JSONObject>() {

						@Override
						public void onResponse(JSONObject arg0) {
							// TODO Auto-generated method stub
							UserInfo userInfo = JsonUtils.jsonToBean(arg0,
									UserInfo.class);
							if (userInfo != null) {
								if (userInfo.getIcon() != null) {
									ImageLoader.getInstance()
											.displayImage(
													VMConst.URL
															+ userInfo
																	.getIcon(),
													iv_userImage,
													MyDisplayImageOptions
															.getInstance());
								} else {
									iv_userImage
											.setImageResource(R.drawable.userhead_bg);
								}
							}
						}

					}, null);
			mQueue.add(jsonObjectRequest);
		}
		if (comment.getCContent() != null) {
			tv_content.setText(comment.getCContent());
		}
		if (comment.getCTime() != null) {
			tv_comment_time.setText(comment.getCTime());
		}
		return linearLayout;
	}
}
